🌎 rns.kin.earth git 🌍
Commit 89de1763854f745b778f3c7a0b016adacaa43fdf
Parents : abae9bf
Author : K8 <k8@taproot.mx>
Signature : Invalid signer <8e4525cda44827204097dbcadf90a94c>, author is <k8@taproot.mx>
Date : 2026-04-11T23:13:59-06:00
Function call logging and other logging improvements.
Changes
Diff
diff --git a/RNS/__init__.py b/RNS/__init__.py
index 79a77468..e0d65b5e 100755
--- a/RNS/__init__.py
+++ b/RNS/__init__.py
@@ -37,6 +37,7 @@ import random
import threading
import math
import bisect
+import inspect
from collections import deque
from threading import Lock, Condition
@@ -76,14 +77,17 @@ LOG_CALLBACK = 0x93
LOG_MAXSIZE = 30*1024*1024
LOG_MAXROT = 9
-loglevel = LOG_NOTICE
-logfile = None
-logdest = LOG_STDOUT
-logcall = None
-logtimestamps = True
-logtimefmt = "%Y-%m-%d %H:%M:%S"
-logtimefmt_p = "%H:%M:%S.%f"
-compact_log_fmt = False
+loglevel = LOG_NOTICE
+logfile = None
+logdest = LOG_STDOUT
+logcall = None
+logtimestamps = True
+logtimefmt = "%Y-%m-%d %H:%M:%S"
+logtimefmt_p = "%Y-%m-%d %H:%M:%S.%f"
+always_precise_time = True
+compact_log_fmt = False
+always_thread_info = True
+disable_func_logs = False
instance_random = random.Random()
instance_random.seed(os.urandom(10))
@@ -128,8 +132,8 @@ def _ensure_log_thread():
_log_thread.start()
def sl(level=3): return loglevel >= level
-def log(msg, level=3, _override_destination = False, pt=False):
- global compact_log_fmt
+def log(msg, level=3, _override_destination = False, pt=False, thread_info=False):
+ global compact_log_fmt, always_precise_time, always_thread_info
if loglevel == LOG_NONE: return
_ensure_log_thread()
@@ -137,10 +141,23 @@ def log(msg, level=3, _override_destination = False, pt=False):
msg = str(msg)
if loglevel >= level:
with _log_cond:
- if pt: logstring = "["+precise_timestamp_str(time.time())+"] "+loglevelname(level)+" "+msg
+ if logtimestamps:
+ if pt or always_precise_time:
+ timestr = "["+precise_timestamp_str(time.time())+"] "
+ else:
+ timestr = "["+timestamp_str(time.time())+"] "
+ else:
+ timestr = ""
+
+ if not compact_log_fmt:
+ logstring = timestr+loglevelname(level)+" "
else:
- if not compact_log_fmt: logstring = ("["+timestamp_str(time.time())+"] " if logtimestamps else "")+loglevelname(level)+" "+msg
- else: logstring = ("["+timestamp_str(time.time())+"] " if logtimestamps else "")+msg
+ logstring = timestr
+
+ if thread_info or always_thread_info:
+ logstring += thread_info_str()+" "
+
+ logstring += msg
_log_queue.append((logstring, level, _override_destination))
_log_cond.notify()
@@ -209,6 +226,29 @@ def _log_job():
if file is not None: file.close()
_log_thread_lock.release()
+def log_func(func):
+ """
+ Decorator to print function call details before and after running, helpful
+ for debugging concurrency issues.
+ """
+
+ if disable_func_logs:
+ return func
+
+ @wraps(func)
+ def wrapper(*args, **kwargs):
+ func_args = inspect.signature(func).bind(*args, **kwargs).arguments
+ func_args_str = ", ".join(map("{0[0]} = {0[1]!r}".format, func_args.items()))
+ log(f"Begin :: {func.__module__}.{func.__qualname__} ( {func_args_str} )", LOG_EXTREME, pt=True, thread_info=True)
+ ret = func(*args, **kwargs)
+ log(f" End :: {func.__module__}.{func.__qualname__} ( {func_args_str} )", LOG_EXTREME, pt=True, thread_info=True)
+ return ret
+
+ return wrapper
+
+def thread_info_str():
+ return f"(P:{os.getpid()} T:{threading.get_ident()})"
+
def rand():
result = instance_random.random()
return result
@@ -731,4 +771,4 @@ from .Packet import PacketReceipt
from .Resolver import Resolver
from .Resource import Resource, ResourceAdvertisement
from .Cryptography import HKDF
-from .Cryptography import Hashes
\ No newline at end of file
+from .Cryptography import Hashes
Served by rngit 1.5.2 - Generated in 0.05s